/*
 * @Descripttion: 
 * @Author: junhuafly
 * @Date: 2020-03-05 13:58:56
 * @LastEditors: junhuafly
 * @Email: junhuafly@163.com
 * @LastEditTime: 2020-04-20 10:33:59
 */
// pages/mine/note/index.js
import {
  noteAPI
} from './../../../api/index'
import {
  returnStatusToast,
  returnStatus
} from './../../../utils/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    confirmDel: false,
    pageNum: 1,
    requestObj: {},
    records: [],
    currentTab: 'note',
    defaultIntr: {
      note: {
        icon: 'me_notes',
        title: '还没有笔记哦～'
      },
      collect: {
        icon: 'common_page',
        title: '暂无收藏笔记哦～'
      },
      draft: {
        icon: 'common_page',
        title: '还没有草稿哦～'
      }
    },
    navbarData:{
      showCapsule:1
    }
  },
  delNote({
    target: {
      dataset: {
        noteid
      }
    }
  }) {
    this.noteId = noteid
    this.setData({
      confirmDel: true
    })
  },
  async onConfirmDel() {
    // 删除笔记
    const params = {
      method: 'delete',
      noteId: this.noteId
    }
    const res = await (this.data.currentTab == 'collect' ? noteAPI.handleNoteLikeOrCollect({
      noteId: this.noteId,
      handle: 1,
      type: 'NO'
    }) : noteAPI.handleNote(params))
    if(res.code===200){
      this.refresh()
      this.getData()
    }

  },
  onChangeTab({
    detail: {
      name
    }
  }) {
    this.setData({
      currentTab: name,
      'navbarData.title':'我的-'+name
    })
    this.refresh()
    this.getData()
  },

  // 获取发布的笔记
  getData(pageSize = 10) {
    const status = this.data.currentTab
    const api = {
      note: 'queryNoteByMe',
      draft: 'queryNoteByMe',
      collect: 'queryByCollectNote'
    }
    return new Promise(async (resolve, reject) => {
      const res = await noteAPI[api[status]]({
        pageNum: this.data.pageNum,
        status: status === 'draft' ? 'DRAFT' : 'PUBLISH',
        pageSize
      })
      const {
        records
      } = await returnStatus(res)
      if (records) {
        this.setData({
          records: [...this.data.records, ...records],
          pageNum: ++this.data.pageNum,
          pages: records.length,
        })
        setTimeout(() => {
          this.setData({
            loadFinish: true
          })
        }, 300);
        resolve(records.length)
      }
      reject(false)

    })
  },

  toDetail({
    target: {
      dataset: {
        noteid: id
      }
    }
  }) {
    wx.navigateTo({
      url: '/pages/note/detail/index?noteId=' + id,
    })
  },

  // 笔记到发布
  toPost({
    target: {
      dataset: {
        noteid
      }
    }
  }) {
    wx.navigateTo({
      url: '/pages/post/index',
      success: (res) => {
        res.eventChannel.emit('params', {
          noteId: noteid
        })
      },
    });
  },
  refresh() {
    this.setData({
      pageNum: 1,
      records: []
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var appInst = getApp();
    appInst.setTitle('我的笔记')
    this.getData()
    this.setData({
      'requestObj': {
        fetchData: (refresh) => {
          refresh ? this.refresh() : ''
          return this.getData()
        },
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})